package org.flik.client.main;

import java.util.List;

import org.flik.client.main.event.TicketChangeEvent;
import org.flik.client.main.service.UserServiceAsync;
import org.flik.client.shared.ClientUtils;
import org.flik.client.shared.CommonAsyncCallback;
import org.flik.server.dto.ProjectData;
import org.flik.server.dto.TextLongPair;
import org.flik.server.dto.TicketChange;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class NewTicket extends Composite {

	private static NewTicketUiBinder uiBinder = GWT
			.create(NewTicketUiBinder.class);

	interface NewTicketUiBinder extends UiBinder<Widget, NewTicket> {
	}
	
	@UiField
	TextBox title;
	
	@UiField
	ListBox ticketType, status, assignedTo; 

	@UiField
	TextArea comment; 
	
	@UiField
	Button createButton, cancelButton, clearButton;

	@UiHandler("cancelButton")
	void onCancelButtonClick(ClickEvent event) {
		hideDialaog();
	}

	@UiHandler("clearButton")
	void onClearButtonClick(ClickEvent event) {
		clearForm();
	}
	
	@UiHandler("createButton")
	void onCreateButtonClick(ClickEvent event) {
		ClientUtils.showRemoteCommunicationMessage("Creating...");
		UserServiceAsync.INSTANCE.create(mapWidgetsToObject(), newTicketProjectId, new CommonAsyncCallback<Long>() {

			@Override
			public void onSuccess(Long result) {
				ClientUtils.hideRemoteCommunicationMessage();
				newTicket.fireEvent(new TicketChangeEvent());
				hideDialaog();
			}
		});
	
	}

	private static void hideDialaog(){
		newTicketEventRegistration.removeHandler();
		dialogBox.hide();
	}
	 
	private TicketChange mapWidgetsToObject() {
		Long assignedToId = Long.parseLong(assignedTo.getValue(assignedTo.getSelectedIndex()));;
		Long ticketTypeId = Long.parseLong(ticketType.getValue(ticketType.getSelectedIndex()));
		Long statusId = Long.parseLong(status.getValue(status.getSelectedIndex()));
		TicketChange ticketChange = new TicketChange(null, assignedToId, ticketTypeId, statusId, title.getValue(), comment.getText());
		return ticketChange;
	}

	public NewTicket() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	private static void fetchAndSetData(Long projectId){
		ClientUtils.showRemoteCommunicationMessage("fetching...");
		UserServiceAsync.INSTANCE.fetchProjectData(projectId, new CommonAsyncCallback<ProjectData>() {

			@Override
			public void onSuccess(ProjectData result) {
				ClientUtils.hideRemoteCommunicationMessage();
				mapDataToWidgets(result);
			}
		});

	}
	
	private static void mapDataToWidgets(ProjectData data){
		List<TextLongPair> ticketTypeList = data.getProjectTicketTypeList();
		newTicket.ticketType.clear();
		for (TextLongPair value:ticketTypeList)
			newTicket.ticketType.addItem(value.getText(), value.getId().toString());
		
		List<TextLongPair> ticketStatusList = data.getProjectTicketStatusList(); 
		newTicket.status.clear();
		for (TextLongPair value:ticketStatusList)
			newTicket.status.addItem(value.getText(), value.getId().toString());
		List<TextLongPair> projectUserList = data.getProjectUserList(); 
		newTicket.assignedTo.clear();
		for (TextLongPair value:projectUserList)
			newTicket.assignedTo.addItem(value.getText(), value.getId().toString());
		ClientUtils.selectListBoxItem(newTicket.assignedTo, Main.getLoggedInUserId());
	}
	private static DialogBox dialogBox = null;
	private static NewTicket newTicket = null;

	private static Long newTicketProjectId;

	private static HandlerRegistration newTicketEventRegistration;
	
	private static void initDialog(){
		dialogBox = new DialogBox(false, true);
		dialogBox.setText("Create new Ticket");
		newTicket = new NewTicket();
		dialogBox.setGlassEnabled(true);
		dialogBox.setAnimationEnabled(true);
		dialogBox.setWidget(newTicket);
	}
	private static void clearForm(){
		newTicket.ticketType.setSelectedIndex(0);
		newTicket.status.setSelectedIndex(0);
		ClientUtils.selectListBoxItem(newTicket.assignedTo, Main.getLoggedInUserId());
		newTicket.comment.setValue("");
		newTicket.title.setValue("");
	}

	public static void show(Long projectId, TicketChangeEvent.Handler handler){
		newTicketProjectId = projectId; 
		if (dialogBox == null)
			initDialog();
		else
			clearForm();
		newTicketEventRegistration = newTicket.addHandler(handler, TicketChangeEvent.TYPE);
		fetchAndSetData(projectId);
		dialogBox.show();
		dialogBox.center();
	}
}
